package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhCoupon;
import com.ruoyi.edh.domain.vo.EdhCouponVo;
import com.ruoyi.edh.service.IEdhCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订货-优惠券Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/coupon")
@Api(description = "易订货-优惠券")
public class EdhCouponController extends BaseController {
    @Autowired(required = true)
    private IEdhCouponService edhCouponService;

    /**
     * 查询易订货-优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:list')")
    @GetMapping("/list")
    @ApiOperation("易订货-优惠券列表")
    public TableDataInfo list(EdhCoupon edhCoupon) {
        startPage();
        List<EdhCouponVo> list = edhCouponService.selectEdhCouponVoList(edhCoupon);
        return getDataTable(list);
    }

    /**
     * 获取可领取的优惠券
     */
    @GetMapping("/availableCoupon")
    @ApiOperation("可领取的优惠券列表")
    public AjaxResult availableCoupon(Long goodsId) {
        List<EdhCouponVo> list = edhCouponService.availableCoupon(goodsId);
        return AjaxResult.success(list);
    }


    /**
     * 导出易订货-优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:export')")
    @Log(title = "易订货-优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-优惠券列表")
    public AjaxResult export(EdhCoupon edhCoupon) {
        List<EdhCouponVo> list = edhCouponService.selectEdhCouponVoList(edhCoupon);
        ExcelUtil<EdhCouponVo> util = new ExcelUtil<EdhCouponVo>(EdhCouponVo.class);
        return util.exportExcel(list, "coupon");
    }

    /**
     * 获取易订货-优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:query')")
    @GetMapping("/{id}")
    @ApiOperation("获取易订货-优惠券详细信息")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(edhCouponService.selectEdhCouponById(id));
    }

    /**
     * 新增易订货-优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:add')")
    @Log(title = "易订货-优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订货-优惠券")
    public AjaxResult add(@RequestBody EdhCoupon edhCoupon) {
        return toAjax(edhCouponService.insertEdhCoupon(edhCoupon));
    }

    /**
     * 修改易订货-优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:edit')")
    @Log(title = "易订货-优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订货-优惠券")
    public AjaxResult edit(@RequestBody EdhCoupon edhCoupon) {
        return toAjax(edhCouponService.updateEdhCoupon(edhCoupon));
    }

    /**
     * 删除易订货-优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:remove')")
    @Log(title = "易订货-优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除易订货-优惠券")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(edhCouponService.deleteEdhCouponByIds(ids));
    }
}
